package com.huanggf.meituan.tabhost;

import android.app.TabActivity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SearchView;
import android.widget.TabHost;
import android.widget.TextView;


import com.huanggf.meituan.R;
import com.huanggf.meituan.fooddetail.FoodDetails;
import com.huanggf.meituan.order.AllOrderActivity;
import com.huanggf.meituan.order.CommentActivity;
import com.huanggf.meituan.order.DrawbackActivity;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class OrderActivity extends TabActivity {

    private SearchView search;
    private TabHost tabHost;
    private LinearLayout recent_purchase;
    private SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.order_layout);
        tabHost = (TabHost) findViewById(android.R.id.tabhost);
        search = (SearchView) findViewById(R.id.search);
        recent_purchase = (LinearLayout) findViewById(R.id.recent_purchase);
        tabHost.addTab(tabHost.newTabSpec("order_all_layout").setIndicator("全部订单")
                .setContent(new Intent(this, AllOrderActivity.class)));
        tabHost.addTab(tabHost.newTabSpec("comment_layout").setIndicator("待评价")
                .setContent(new Intent(this, CommentActivity.class)));
        tabHost.addTab(tabHost.newTabSpec("drawback_layout").setIndicator("退款")
                .setContent(new Intent(this, DrawbackActivity.class)));
        db = SQLiteDatabase.openOrCreateDatabase("/data/data/com.huanggf.meituan/databases/meituan.db", null);
        //最近买过
        List<Map<String, Object>> foodShopsAndCountSizes = getFoodShopByOrderDesc();
        for (Map<String, Object> foodShopsAndCountSize : foodShopsAndCountSizes) {
            View order_recent_layout = LayoutInflater.from(this).inflate(R.layout.order_recent_layout, null);
            ImageView shop_logo = (ImageView) order_recent_layout.findViewById(R.id.shop_logo);
            TextView shop_name = (TextView) order_recent_layout.findViewById(R.id.shop_name);
            TextView purchase_count_shop = (TextView) order_recent_layout.findViewById(R.id.purchase_count_shop);
            FoodDetails foodDetails = (FoodDetails) foodShopsAndCountSize.get("foodDetails");
            Integer count_size = (Integer) foodShopsAndCountSize.get("count_size");
            try {
                if (null != FoodDetails.getShopLogo() && !FoodDetails.getShopLogo().trim().equals(""))
                    shop_logo.setImageBitmap(BitmapFactory.decodeStream(new FileInputStream(FoodDetails.getShopLogo())));
                shop_name.setText(FoodDetails.getShopName());
                purchase_count_shop.setText(String.valueOf(count_size));
                recent_purchase.addView(order_recent_layout);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }

    }

        private List<Map<String, Object>> getFoodShopByOrderDesc() {
            String selOrderShop = "SELECT f.shop_logo,f.shop_name,count(o.food_id) count_size FROM m_order o INNER JOIN food_detail f ON o.food_id=f.id GROUP BY o.food_id ORDER BY count(o.food_id) DESC";
            Cursor c = db.rawQuery(selOrderShop, null);
            List<Map<String, Object>> foodShopsAndCountSizes = new ArrayList<>();
            while (c.moveToNext()) {
                Map<String, Object> foodShopsAndCountSize = new HashMap<>();
                FoodDetails foodDetails = new FoodDetails();
                foodDetails.setShopLogo(c.getString(0));
                foodDetails.setShopName(c.getString(1));
                foodShopsAndCountSize.put("foodDetails", foodDetails);
                foodShopsAndCountSize.put("count_size", c.getInt(2));
                foodShopsAndCountSizes.add(foodShopsAndCountSize);
            }
            c.close();
            return foodShopsAndCountSizes;
        }
    }